/*
 * Created on 2005-11-11
 *
 */
package com.yl.cap.analyst.report.chart.maker;

import java.awt.Color;
import java.io.FileOutputStream;

import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartUtilities;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.AxisLocation;
import org.jfree.chart.axis.CategoryAxis;
import org.jfree.chart.axis.CategoryLabelPositions;
import org.jfree.chart.axis.NumberAxis;
import org.jfree.chart.block.BlockBorder;
import org.jfree.chart.block.BlockContainer;
import org.jfree.chart.block.BorderArrangement;
import org.jfree.chart.block.EmptyBlock;
import org.jfree.chart.labels.StandardCategoryToolTipGenerator;
import org.jfree.chart.plot.CategoryPlot;
import org.jfree.chart.plot.DatasetRenderingOrder;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.renderer.category.LineAndShapeRenderer;
import org.jfree.chart.title.CompositeTitle;
import org.jfree.chart.title.LegendTitle;
import org.jfree.data.category.CategoryDataset;
import org.jfree.data.general.DatasetUtilities;
import org.jfree.ui.RectangleEdge;
import org.jfree.ui.RectangleInsets;

import com.yl.cap.analyst.report.chart.data.ChartData;
import com.yl.cap.analyst.report.chart.data.DualAxisChartData;

public class DualAxisMaker extends ChartMaker
{

    public void makeChart(String fileName, ChartData data) throws Exception
    {
        DualAxisChartData chartData = (DualAxisChartData)data;
        //create dataset
        CategoryDataset barDataset = DatasetUtilities.createCategoryDataset(
                chartData.getBarData().getRowKeys(), chartData.getBarData().getColumnKeys(), chartData.getBarData().getData());
        CategoryDataset lineDataSet = DatasetUtilities.createCategoryDataset(
                chartData.getLineData().getRowKeys(), chartData.getLineData().getColumnKeys(), chartData.getLineData().getData());
        //================================================
        JFreeChart jfreechart = ChartFactory.createBarChart(data.getCaption(), chartData.getXCaption(), chartData.getYCaption(), barDataset, PlotOrientation.VERTICAL, false, true, false);
        jfreechart.setBackgroundPaint(Color.white);
        CategoryPlot categoryplot = (CategoryPlot)jfreechart.getPlot();
        categoryplot.setBackgroundPaint(new Color(238, 238, 255));
        categoryplot.setDomainAxisLocation(AxisLocation.BOTTOM_OR_RIGHT);
    
        categoryplot.setDataset(1, lineDataSet);
        categoryplot.mapDatasetToRangeAxis(1, 1);
        CategoryAxis categoryaxis = categoryplot.getDomainAxis();
        categoryaxis.setCategoryLabelPositions(CategoryLabelPositions.DOWN_45);
        NumberAxis numberaxis = new NumberAxis(chartData.getZCaption());
        categoryplot.setRangeAxis(1, numberaxis);
        LineAndShapeRenderer lineandshaperenderer = new LineAndShapeRenderer();
        lineandshaperenderer.setToolTipGenerator(new StandardCategoryToolTipGenerator());
        categoryplot.setRenderer(1, lineandshaperenderer);
        categoryplot.setDatasetRenderingOrder(DatasetRenderingOrder.FORWARD);
        LegendTitle legendtitle = new LegendTitle(categoryplot.getRenderer(0));
        legendtitle.setMargin(new RectangleInsets(2D, 2D, 2D, 2D));
        legendtitle.setBorder(new BlockBorder());
        LegendTitle legendtitle1 = new LegendTitle(categoryplot.getRenderer(1));
        legendtitle1.setMargin(new RectangleInsets(2D, 2D, 2D, 2D));
        legendtitle1.setBorder(new BlockBorder());
        BlockContainer blockcontainer = new BlockContainer(new BorderArrangement());
        blockcontainer.add(legendtitle, RectangleEdge.LEFT);
        blockcontainer.add(legendtitle1, RectangleEdge.RIGHT);
        blockcontainer.add(new EmptyBlock(2000D, 0.0D));
        CompositeTitle compositetitle = new CompositeTitle(blockcontainer);
        compositetitle.setPosition(RectangleEdge.BOTTOM);
        jfreechart.addSubtitle(compositetitle);

        FileOutputStream fos_jpg = null;
        try
        {
            fos_jpg = new FileOutputStream(fileName);
            ChartUtilities.writeChartAsJPEG(fos_jpg, 100, jfreechart, data
                    .getWidth(), data.getHeight(), null);
        }
        catch (Exception e)
        {
            fos_jpg.close();
            throw e;
        }
        fos_jpg.close();
    }
}
